
#include "Frame.h"

Frame::Frame(SGD::Point anchor, SGD::Rectangle draw, SGD::Rectangle coll, float curTime, float maxTime, int soundID, bool flipped)
{
	SetAnchorPoint(anchor);
	if (flipped)
	{
		SetReverseDrawRect(draw);
		SetReverseCollisionRect(coll);
	}
	else
	{
		SetDrawRect(draw);
		SetCollisionRect(coll);
	}
	fCurTime = curTime;
	fMaxTime = maxTime;
	iSoundID = soundID;
	IsFlipped(flipped);
}

Frame::~Frame()
{
}

// Accessors
bool Frame::IsFlipped()
{
	return bFlipped;
}

SGD::Rectangle Frame::GetDrawRect()const
{
	return drawRect;
}

SGD::Point Frame::GetDestDrawPoint(SGD::Point position) const
{
	SGD::Point point;
	point.x = position.x - fDrawLeft;
	point.y = position.y - fDrawTop;
	
	return point;
}

SGD::Rectangle Frame::GetCollisionRect(SGD::Point position)
{
	SGD::Rectangle rect;
	rect.left = position.x - fCollLeft;
	rect.top = position.y - fCollTop;
	rect.right = position.x + fCollRight;
	rect.bottom = position.y + fCollBottom;
	return rect;
}


SGD::Point Frame::GetAnchorPoint() const
{
	return anchorPoint;
}

float Frame::GetCurrentTime()
{
	return fCurTime;
}

float Frame::GetMaxTime()
{
	return fMaxTime;
}

int Frame::GetSoundID()
{
	return iSoundID;
}

float Frame::GetCollisionTop()
{
	return fCollTop;
}

float Frame::GetCollisionRight()
{
	return fCollRight;
}

float Frame::GetCollisionLeft()
{
	return fCollLeft;
}

// Mutators
void Frame::IsFlipped(bool flipped)
{
	bFlipped = flipped;
}

void Frame::SetDrawRect(SGD::Rectangle draw)
{
	drawRect = draw;
	fDrawLeft = GetAnchorPoint().x - GetDrawRect().left;
	fDrawTop = GetAnchorPoint().y - GetDrawRect().top;
}

void Frame::SetCollisionRect(SGD::Rectangle coll)
{
	fCollLeft = GetAnchorPoint().x - coll.left;
	fCollTop = GetAnchorPoint().y - coll.top;
	fCollRight = coll.right - GetAnchorPoint().x;
	fCollBottom = coll.bottom - GetAnchorPoint().y;
}

void Frame::SetReverseDrawRect(SGD::Rectangle draw)
{
	drawRect = draw;

	SGD::Rectangle sourceRect = GetDrawRect();
	SGD::Point anchorSource = GetAnchorPoint();

	fDrawLeft = GetAnchorPoint().x - GetDrawRect().left
		- ((sourceRect.right - sourceRect.left)	- (sourceRect.right - anchorSource.x)) * 2;
	fDrawTop = GetAnchorPoint().y - GetDrawRect().top;
}

void Frame::SetReverseCollisionRect(SGD::Rectangle coll)
{
	fCollLeft = coll.right - GetAnchorPoint().x;
	fCollTop = GetAnchorPoint().y - coll.top;
	fCollRight = coll.ComputeWidth() - fCollLeft;
	fCollBottom = coll.bottom - GetAnchorPoint().y;
}

void Frame::SetAnchorPoint(SGD::Point anchor)
{
	anchorPoint = anchor;
}

void Frame::SetCurrentTime(float curTime)
{
	fCurTime = curTime;
}

void Frame::SetMaxTime(float maxTime)
{
	fMaxTime = maxTime;
}

void Frame::SetSoundID(int soundID)
{
	iSoundID = soundID;
}
